// ------Register module
import { GetterTree, MutationTree, ActionTree } from 'vuex';
import Http, { Api } from '@/request';
import { RegisterState } from './register.interface';
import { LoginState } from '../login/login.interface';

const state: RegisterState = {
  username: '',
  password: '',
  email: '',
  name: ''
  // phone: ''
};

// 强制使用getter获取state
const getters: GetterTree<RegisterState, any> = {
  // username(states): string {
  //     return states.username
  // },
  // password(states): string {
  //     return states.password
  // },
  // email(states): string {
  //     return states.email
  // },
  // name(states): string {
  //     return states.name
  // }
};

// 更改state
const mutations: MutationTree<RegisterState> = {
  // 更新state都用该方法
  // UPDATE_STATE(states: any, payload: RegisterState) {
  //     states.username = payload.username;
  //     states.password = payload.password;
  //     states.email = payload.email;
  //     states.name = payload.name;
  // }
};

const actions: ActionTree<RegisterState, any> = {
  // UPDATE_STATE_ASYN({commit}, data: string) {
  //     commit('UPDATE_STATE', {
  //         username: data
  //     })
  // },
  registerHttp({ commit }, data: RegisterState) {
    return new Promise((resolve, reject) => {
      Http.post(Api.REGISTER, data)
        .then(res => {
          console.log('register===>>>', Api.REGISTER);
          // 修改数据状态  当然 在登录接口可能用不到
          // commit('UPDATE_STATE', data);
          // 只返回数据
          resolve(res);
        })
        .catch(() => {
          reject();
        });
    });
  }
};

export default {
  state,
  getters,
  mutations,
  actions
};
